package com.tiyende.kash.loop.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * DateUtil
 * @Author wwdeng
 * @CreateTime: 2024/11/28 11:18
 * @Annotation：
 */
public class TiyendeDateUtil {

    /**
     * 判断是否为今天(效率比较高)
     * @param timestamp 传入的 时间戳 毫秒
     * @return true今天 false不是
     * @throws Exception
     */
    public static boolean IsToday(Long timestamp)  {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        try {
            Date date = new Date(timestamp);
            cal.setTime(date);

            if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
                int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
                if (diffDay == 0) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 两个时间戳是否为同一天
     * @param timestamp1
     * @param timestamp2
     * @return
     */
    public static boolean isSameDay(long timestamp1, long timestamp2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(timestamp1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTimeInMillis(timestamp2);

        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
                cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 判断是否为昨天(效率比较高)
     * @param timestamp 传入的时间戳 毫秒
     * @return true昨天 false不是
     * @throws Exception
     */
    public static boolean IsYesterday(Long timestamp)  {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        try {
            Date date = new Date(timestamp);
            cal.setTime(date);
            if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
                int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
                if (diffDay == -1) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 获取两个月后的时间戳
     */
    public static long getTwoMonthsLaterTimestamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 2); // 在当前日期上加上两个月
        return calendar.getTimeInMillis(); // 获取时间戳
    }

    /**
     * 计算两个时间戳相差多少天
     * @param timestamp1
     * @param timestamp2
     * @return
     */
    public static int getDaysFromTimestamps(long timestamp1, long timestamp2) {
        // 将时间戳转换为日期
        Date date1 = new Date(timestamp1);
        Date date2 = new Date(timestamp2);
        // 获取两个日期之间的差异
        long diff = date2.getTime() - date1.getTime();
        // 将差异转换为天数
        return (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
    }

    /**
     * 判断是否是同一年
     * @param timestamp1
     * @param timestamp2
     * @return
     */
    public boolean areTimestampsFromSameYear(long timestamp1, long timestamp2) {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();

        // 将时间戳设置到calendar对象中
        calendar1.setTimeInMillis(timestamp1);
        calendar2.setTimeInMillis(timestamp2);

        // 获取年份并比较
        int year1 = calendar1.get(Calendar.YEAR);
        int year2 = calendar2.get(Calendar.YEAR);

        return year1 == year2;
    }

    /**
     * yyyy-MM-dd 转时间戳
     * @param dateString
     * @return
     */
    public static long convertDateToTimestamp(String dateString) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date = dateFormat.parse(dateString);
            assert date != null;
            return date.getTime();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 指定格式日期 转时间戳
     * @param dateString
     * @return
     */
    public static long convertDateToTimestamp(String pattern,String dateString) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
            Date date = dateFormat.parse(dateString);
            assert date != null;
            return date.getTime();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 格式化时间
     */
    public static String getFormatTime(long timestamp,String pattern){
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpFormat = new SimpleDateFormat(pattern);
        Date date = new Date(timestamp);
        return simpFormat.format(date);
    }
}
